var express = require('express');
var mysql  = require('mysql');  //调用MySQL模块
var router = express.Router();

/* GET users listing. */
router.post('/', function(req, res, next) {
	
	var name = req.body.name; 
    var email = req.body.email;
	var age = req.body.age;
	
//创建一个connection
var connection = mysql.createConnection({     
  host     : '127.0.0.1',       //主机
  user     : 'root',               //MySQL认证用户名
  password : 'root',        //MySQL认证用户密码
  port: '3306',                   //端口号
  database:'test',
}); 

connection.connect();

var  userAddSql = 'INSERT INTO user(username,email,age) VALUES(?,?,?)';
var  userAddSql_Params = [name, email, age];
//增
connection.query(userAddSql,userAddSql_Params,function (err, result) {
        if(err){
         console.log('[INSERT ERROR] - ',err.message);
         return;
        }        

       console.log('--------------------------INSERT----------------------------');
       console.log('INSERT ID:',result.insertId);        
       console.log('INSERT ID:',result);        
       console.log('-----------------------------------------------------------------\n\n');  
});

connection.end();






   // 输出 JSON 格式  
   data = {  
       'name':req.body.name,  
       'email':req.body.email ,
	   'age':req.body.age 
   };  
   console.log(data);  
  //  response.end(JSON.stringify(data));  
   res.json(data); 
   
});

module.exports = router;


